package com.company;

import com.sun.xml.internal.ws.api.model.wsdl.WSDLOutput;

import java.util.ArrayList;
import java.util.Collections;

public class Main {

    // 1. 将数据保存到程序中【List】
    // 遍历输出所有数据
    // 计算语文的最高分
    // 计算数学的平均分
    // 统计男生和女生的人数
    // 根据英语成绩进行排序输出【从高到低】
    // …………………
        static ArrayList<StudentScore> studentScores = new ArrayList<>();

        public static void main(String[] args) {
            addStudenScores();

            System.out.println("=== 所有学生成绩 ===");
            showAll();

            System.out.println("\n=== 语文最高分 ===");
            System.out.println(getMaxLanguage());

            System.out.println("\n=== 数学平均分 ===");
            System.out.printf("%.2f\n", getAverageMath());

            System.out.println("\n=== 性别统计 ===");
            countSex();


            System.out.println("\n=== 英语成绩排序（从高到低） ===");
            getMaxLanguage();
            showAll();

            System.out.println("\n=== 语文成绩排序（从低到高） ===");
            sortByChineseAscending();
            showAll();
        }

    // 新增的语文升序排序方法



    private static void addStudenScores() {
            studentScores.add(new StudentScore("001", "予安", "男", 86, 91, 88));
            studentScores.add(new StudentScore("002", "若初", "女", 88, 92, 85));
            studentScores.add(new StudentScore("003", "清禾", "女", 90, 87, 93));
            studentScores.add(new StudentScore("004", "晨曦", "男", 82, 88, 87));
            studentScores.add(new StudentScore("005", "景行", "男", 85, 95, 89));
            studentScores.add(new StudentScore("006", "云起", "女", 92, 88, 94));
            studentScores.add(new StudentScore("007", "砚冰", "男", 78, 90, 87));
            studentScores.add(new StudentScore("008", "时安", "女", 89, 86, 91));
        }

        public static int getMaxLanguage() {
            return studentScores.stream()
                    .mapToInt(StudentScore::getLanguage)
                    .max()
                    .orElse(0);
        }

        public static double getAverageMath() {
            return studentScores.stream()
                    .mapToInt(StudentScore::getMath)
                    .average()
                    .orElse(0.0);
        }

        public static void countSex() {
            long male = studentScores.stream()
                    .filter(s -> "男".equals(s.getSex()))
                    .count();
            long female = studentScores.stream()
                    .filter(s -> "女".equals(s.getSex()))
                    .count();
            System.out.println("男生：" + male + "人 | 女生：" + female + "人");
        }

        public static void sortByEnglish() {
            Collections.sort(studentScores, (s1, s2) -> s2.getEnglish() - s1.getEnglish());
        }

      public static void sortByChineseAscending() {
        Collections.sort(studentScores, (s1, s2) -> s1.getLanguage() - s2.getLanguage());
       }

        public static void showAll() {
            studentScores.forEach(System.out::println);
        }
    }